import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
     component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录',
        hidden: true, // 隐藏在导航栏中
        // fullscreen: true // 全屏显示
      }
    },
    {
      path:"/chat",
      name: "chat",
      component: () => import('@/views/chat/index.vue'),
      meta: {
        title: '聊天',
        icon: 'fas fa-comments',
        hidden: true, // 隐藏在导航栏中 

      }
    },
    {
      path:"/editor",
      name: "editor",
      component: () => import('@/views/editor/index.vue'),
      meta: {
       title: '写文章 - 拾壹博客',
        icon: 'fas fa-edit',
        requireAuth: true, // 需要登录才能访问
        hidden: true
      }
    },
    {
      path: '/404',
      name: "404",
      component: () => import('@/views/404/index.vue'),
    },
    {
      path: '/',  // 以 / 开头的路径表示根路径
      redirect: '/layout/home',
    },


    // 布局路由
    {
      path:"/layout",
      name: "layout",
      redirect: '/layout/home',
      component: () => import('@/views/layout/index.vue'),
      meta: {
        title: '布局',
        // 打开后面会有一个 loading (等待) 的动画
        loading: true,
      },
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页',
            transition: 'fade',
            icon: 'fas fa-home',
            loading: true,
            requireAuth: true, // 需要登录才能访问

          }
        },
        {
          path:"archive",
          name: "archive",
          component: () => import('@/views/archives/son/archive.vue'),
          meta: { 
            transition: 'fade',
            title: '归档 - 乐乐博客',
            icon: 'fas fa-archive'
          }
        },
        {
          path:"classify",
          name: "classify",
          component: () => import('@/views/archives/son/classification.vue'),
          meta: {
           transition: 'fade',
            title: "分类 - 乐乐博客",
            icon: 'fas fa-folder'
          }
        },
        {
          path:"label",
          name: "label",
          component: () => import('@/views/archives/son/label.vue'),
          meta: {
            transition: 'fade',
            title: '标签 - 乐乐博客',
            icon: 'fas fa-tags'
          }
        }, 
         {  
          path:"takeAbout",
          name: "takeAbout",
          component: () => import('@/views/takeAbout/index.vue'),
          meta: {
            title: '说说 - 乐乐博客',
            icon: 'fas fa-comment-dots'

          }
        },
         {
          path:"photo",
          name: "photo",
          component: () => import('@/views/photo/index.vue'),
          meta: {
            transition: 'fade',
            title: '相册 -乐乐博客',
            icon: 'fas fa-images'
          }
        },
        {
          path:"hortSearch",
          name: "hortSearch",
          component: () => import('@/views/hortSearch/index.vue'),
          meta: {
            transition: 'fade',
            title: '热搜 - 乐乐博客',
            icon: 'fas fa-fire'
          }
        },
        {
          path:"research",
          name: "research",
          component: () => import('@/views/research/index.vue'),
          meta: {
            transition: 'fade',
            title: '资源 - 乐乐博客',
            icon:"far fa-download"
          }
        },
         {
          path:"messageBoard",
          name: "messageBoard",
          component: () => import('@/views/messageBoard/index.vue'),
          meta: {
            transition: 'fade',
            title: '留言板 - 乐乐博客',
            icon: 'fas fa-comments'
          }
        },
       
        {
          path:"link",
          name: "link",
          component: () => import('@/views/link/index.vue'),
          meta: {
            transition: 'fade',
            title: '友情链接 - 乐乐博客',
            icon: 'fas fa-user-friends'
          }
        },
        {
          path:"user",
          name: "user",
          component: () => import('@/views/user/index.vue'),
          meta: {
            title: '用户',
            hidden: true, // 隐藏在导航栏中
          },
          children: [
             {
                path: 'user/profile',
                name: 'userProfile',
                component: () => import('@/views/profile/index.vue'),
                meta: {
                  title: '个人中心',
                  icon: 'fas fa-user',
                  requireAuth: true, // 需要登录才能访问
                }
              }
            ]
        },

         {
          path:"aboutme",
          name: "aboutme",
          component: () => import('@/views/about/son/aboutme.vue'),
          meta: {
             transition: 'fade',
             title: '关于本站 - 乐乐博客',
             icon: 'fas fa-info-circle'
          }
        },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 检测目标页面是否需要登录
  if(to.meta.requireAuth){
    // 如果需要登录，检查用户是否已登录 
    if (localStorage.getItem('pinia_token')) {
      next();
    } else {
      next('/login');
    }
  } else {
    // 如果不需要登录，直接放行
    next()
  }
    
  
});

export default router
